package com.example.campuslink.commen.adpter

import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.databinding.AdapterHaveBtnUserInfoBinding
import com.example.campuslink.viewModel.FansAndAttentionViewModel

class FansListAdapter(vm: FansAndAttentionViewModel,nowNumber:String) :
    CoreAdapter<AdapterHaveBtnUserInfoBinding, FansAndAttentionViewModel, String>(
        BR.data,
        BR.viewModel,
        vm
    ) {
        private val nowNumber = nowNumber
    override fun getItemLayoutId(viewType: Int): Int {
        mViewModel?.getAttentionListAndFansList(nowNumber)
        return R.layout.adapter_have_btn_user_info
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val userNumber = mListData[position]
        holder.binding.apply {
            // 设置默认值
            root.findViewById<TextView>(R.id.tv_userName).text = "加载中..."
            Glide.with(holder.itemView.context).load(R.drawable.ic_empty)
                .apply(RequestOptions.circleCropTransform())
                .into(root.findViewById<ImageView>(R.id.iv_userImg))

            if (nowNumber !=
                Sp.getString(holder.itemView.context, SpKey.USER_NUMBER.key, "")
            ) {
                root.findViewById<Button>(R.id.btn_relationship).visibility = View.GONE
                root.findViewById<Button>(R.id.btn_relationship).isEnabled = false
                if (Sp.getString(
                        holder.itemView.context,
                        SpKey.USER_NUMBER.key,
                        ""
                    ) == mListData[position]
                ) {
                    root.findViewById<Button>(R.id.btn_relationship).text = "我"
                }
            }

            if (mViewModel?.attentionList?.value?.contains(userNumber) ?: false) {

                root.findViewById<Button>(R.id.btn_relationship).text = "朋友"
                root.findViewById<Button>(R.id.btn_relationship).setOnClickListener({
                    mViewModel?.removeAttentionAndFans(userNumber, nowNumber = nowNumber )
                    root.findViewById<Button>(R.id.btn_relationship).text = "加载中..."
                    notifyChange()
                })
            } else {
                root.findViewById<Button>(R.id.btn_relationship).text = "回关"
                root.findViewById<Button>(R.id.btn_relationship).setOnClickListener({
                    mViewModel?.addAttentionAndFans(userNumber,nowNumber)
                    root.findViewById<Button>(R.id.btn_relationship).text = "加载中..."
                    notifyChange()
                })
            }


            // 获取用户信息
            mViewModel?.getUserInfo(userNumber)

            // 观察数据变化 - 使用 viewLifecycleOwner 或 holder.itemView 的生命周期
            mViewModel?.userInfoMap?.observe(holder.itemView.context as LifecycleOwner) { map ->
                map[userNumber]?.let { info ->
                    root.findViewById<TextView>(R.id.tv_userName).text = info.userName
                    Glide.with(holder.itemView.context).load(info.userAvatar)
                        .error(R.drawable.ic_empty)
                        .into(root.findViewById<ImageView>(R.id.iv_userImg))
                }
            }
            if (position == mListData.size - 1) {
                mViewModel?.getUserInfo(userNumber)
            }


            root.setOnClickListener({
                mViewModel?.jumpToUserInfoActivity(userNumber)
            })
        }

    }
}